package com.jameskarl.amap.map.apis

import android.util.Log
import com.amap.api.location.AMapLocation
import com.amap.api.maps.AMap
import com.autonavi.amap.mapcore.Inner_3dMap_location
import com.jameskarl.amap.AmapPlugin
import com.jameskarl.amap.map.ReplyToFlutter
import com.jameskarl.amap.map.bean.toMapLocation

class LocationApi : FlutterApi {

    override fun handle(methodId: String, map: AMap, data: Any?): ReplyToFlutter? {
        return when (methodId) {
            "getMyLocation" -> getMyLocation(map, data)
            else -> null
        }
    }

    private fun getMyLocation(map: AMap, data: Any?): ReplyToFlutter? {
        map.myLocation?.let { location ->
            if (AmapPlugin.showDebugMessage) Log.d("MAP", "myLocation is $location")
            return when (location) {
                is Inner_3dMap_location -> {
                    ReplyToFlutter.Success(data = location.toMapLocation())
                }
                is AMapLocation -> {
                    ReplyToFlutter.Success(data = location.toMapLocation())
                }
                else -> {
                    ReplyToFlutter.Success(data = location.toMapLocation())
                }
            }
        }
        return ReplyToFlutter.Failed(message = "获取当前位置信息失败")
    }
}